<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/4
 * Time: 14:09
 */

namespace Home\Controller;

use Think\Controller;
require_once APP_CLASS.'PHPExcel.php';
require_once APP_CLASS.'PHPExcel/Writer/Excel5.php';     // 用于其他低版本xls
require_once APP_CLASS.'PHPExcel/Writer/Excel2007.php'; // 用于 excel-2007 格式
//require_once VENDOR_PATH . 'PHPExcel.php';

class ExcelController extends Controller
{
    public function __construct() {
        parent::__construct();
        set_time_limit(0);
    }
    public function index()
    {
        $pattern='/^0{0,1}(13[0-9]|15[7-9]|153|156|17[0-9]|18[7-9])[0-9]{8}$/';
        echo preg_match($pattern,130418105545);
    }
    /**
     *读取xls内容并存储进数据库
     */
    public function readxls(){
        $filePath = "D:/fs.xls";
        $type='服饰';
        $pattern='/^0{0,1}(13[0-9]|15[7-9]|153|156|17[0-9]|18[7-9])[0-9]{8}$/';
        $PHPReader = new \PHPExcel();
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath);
        $objWorksheet = $objPHPExcel->getActiveSheet();
        $objActSheetArr = $objWorksheet->toArray('',false,true,false);
        // p($objActSheetArr);
        // die();
        foreach ($objActSheetArr as $key => $value) {
            if(!$key||!preg_match($pattern,$value[3]))continue;
            $data=array(
                'company_name'=>$value[0],
                'user_name'=>$value[1],
                'phone_num'=>$value[3],
                'address'=>$value[4],
                'product'=>$value[5],
                // 'industry'=>$value[12],
                // 'model'=>$value[14],
                // 'introduction'=>$value[15],
                // 'company_type'=>$value[16],
                // 'homepage'=>$value[19]
            );
            if($value[1]=='--'){
                $data['user_name']=mb_substr($value[0],0,4);
            }
            $data['type']=$type;
            M('contact_company')->add($data);
            echo $key.'<br>';
            flush();
            usleep(100);
        }
    }
}